//
//  HOReportEditViewController.m
//  EQC
//
//  Created by foguo-mac-1 on 15/11/2017.
//  Copyright © 2017 WeiDu. All rights reserved.
//

#import "HOReportEditViewController.h"
#import "HOUtils.h"
#import "HOVoiceDownloader.h"
#import "HOConstants.h"

@interface HOReportEditViewController ()
{
	BOOL isInitedData;
}
@end

@implementation HOReportEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"编辑报修单", @"编辑报修单")];
	[self displayRightButtonItemWithTitle:@""];
	[self initData];
	[baseTableview reloadData];
}

- (void)initData
{
	if(isInitedData)
		return;
	isInitedData = YES;
	[self.reportDescEditView.getTextView setText:[HOUtils getValideString:self.reportEntity.detail]];
//	[self.recordView set]
	
	self.selectedFirstAreaEntity = self.reportEntity.firstAreaEntity;
	self.selectedSecondAreaEntity = self.reportEntity.secondAreaEntity;
	self.selectedThirdAreaEntity = self.reportEntity.thirdAreaEntity;
	
	self.selectedFaultGategoryEntity = self.reportEntity.firstTypeEntity;
	self.selectedSubFaultGategoryEntity = self.reportEntity.secondTypeEntity;
	
	self.selectedDepartmentEntity = self.reportEntity.firstDepartmentEntity;
	self.selectedSubDepartmentEntity = self.reportEntity.secondDepartmentEntity;
	
	
	self.voiceFileURL = self.reportEntity.voiceURL;
	self.isNeedRerepair = self.reportEntity.isResubmit == 1;
	self.totalTime = self.reportEntity.totalTime;
	self.uploadImgView.needDisplayRemoveButton = YES;
	[self.uploadImgView setImagePaths:self.reportEntity.imageList];
	[self downloadVoice];
}


- (void)submit
{
	[super submit];
	NSDictionary *dic = [self getReportDic];
	if(dic == nil)
		return;
	NSMutableDictionary *wrapDic = [NSMutableDictionary dictionaryWithDictionary:dic];
	[wrapDic setObject:[NSNumber numberWithInt:self.reportEntity.orderId] forKey:kHO_FIELD_OrderId];
	[self sendRequestWithParams:wrapDic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)downloadVoice
{
	//		self.voiceFileURL = @"http://116.255.168.232:8088/Uploads/Voice/2016-07-04/1110d5a181ed17b0b4fe454ce1de8692.amr";//ios record
	//	self.voiceFileURL = @"http://116.255.168.232:8088/Uploads/Voice/2016-07-06/8d261d617b128a86b38d634068464995.amr";//android record
	//	self.totalTime = 5;
	//	NSLog(@"self.voiceFileURL:%@", self.voiceFileURL);
	if([HOUtils getValideString:self.voiceFileURL].length > 0 && self.totalTime > 0)
	{
		[self.recordView showLoadingWithTotalTime:self.totalTime];
		[[HOVoiceDownloader sharedInstance] downloadFielWithURL:self.voiceFileURL];
	}
}

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetUserInfo])
	{
		[super requestDidSuccessWithResponse:response];
	}
	else if([response isEqualAction:kHO_ACTION_AddNormalReport])
	{
		[self cleanup];
		[self sendCommand:kHO_CMD_Report_Detail_Changed data:nil];
		[baseTableview reloadData];
		[self popupHintView:@"编辑成功" inTime:2 needCallback:YES];
	}
}

- (void)hintDidHide
{
	[self goBack];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
